package com.vp.org.web.servlet;

import com.vp.org.dao.OrdersDao;
import com.vp.org.dao.impl.OrdersDaoImpl;
import com.vp.org.pojo.AdminInfo;
import com.vp.org.pojo.Orders;
import com.vp.org.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Orders")
public class OrdersServlet extends BaseServlet {

    private Orders orders = null;
    private OrdersDao ordersDao = null;
    private AdminInfo adminInfo = null;

    public void deleteOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        orders = new Orders();
        ordersDao = new OrdersDaoImpl();

        orders.setId(Integer.parseInt(id));

        if (ordersDao.deleteOrders(orders) > 0) {
            // 删除成功
            response.sendRedirect("http://localhost:8080/Orders?method=findByPage&pageIndex3=1");
        } else {
            // 删除失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收用户点击的上一页，下一页或者是指定的页数
        int pageIndex3 = Integer.parseInt(request.getParameter("pageIndex3"));

        ordersDao = new OrdersDaoImpl();

        adminInfo = new AdminInfo();
        adminInfo.setAdmin("admin");

        // 订单分页查询,默认第一页
        request.getSession().setAttribute("orders",ordersDao.ordersPageQuery(adminInfo,pageIndex3));
        // 订单分页总页数
        request.getSession().setAttribute("ordersPage",ordersDao.ordersPage());
        // 订单存储是第几页
        request.getSession().setAttribute("currentPage3",pageIndex3);

        // 地址重定向
        response.sendRedirect("admin/ordersQuery.jsp");
    }
}
